//
//  SecondViewController.m
//  Cook
//
//  Created by 0.0 on 15-4-16.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "SecondViewController.h"
#import "SecondTableViewCell.h"
#import "SecondModel.h"
#import "ResultViewController.h"

#define officeURL @"http://121.41.87.4/HandheldKitchen/api/vegetable/tbldisease!getDisease.do"



@interface SecondViewController () <UITableViewDataSource, UITableViewDelegate>
{
    UITableView *_tabelView;
}

@property (nonatomic, retain) NSMutableArray *secondArray;
@property (nonatomic, copy) NSString *secondOfficeId;

@end

@implementation SecondViewController

- (void)dealloc
{
    [_secondOfficeId release];
    [_secondArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.secondArray = [NSMutableArray array];
    }
    
    return self;
}

+ (SecondViewController *)secondViewControllerWithOfficeId:(NSString *)officeId
{
    SecondViewController *view = [[SecondViewController alloc] init];
    view.secondOfficeId = officeId;
    
    return [view autorelease];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatBackground];
    [self createTableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
}

- (void)creatBackground
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [imageView setImage:[UIImage imageNamed:@"secondBarBacground.jpg"]];
    [self.view addSubview:imageView];
    [imageView release];
}


- (void)createTableView
{
    _tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 44) style:UITableViewStylePlain];
    
    _tabelView.rowHeight = 60;
    [_tabelView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"secondCell"];
    
    _tabelView.backgroundColor = [UIColor clearColor];
    
    [_tabelView setDelegate:self];
    [_tabelView setDataSource:self];
    
    [self.view addSubview:_tabelView];
    [_tabelView release];
    
    // 进行网络请求
    [self startConnect];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.secondArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondCell"];
    
    SecondModel *model = [self.secondArray objectAtIndex:indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"imagePlace" ofType:@"jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    [cell.secondImageView sd_setImageWithURL:[NSURL URLWithString:model.imageName] placeholderImage:image];
    [cell.secondLabel setText:model.diseaseName];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.secondLabel.textColor = [UIColor whiteColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ResultViewController *resultView = [[ResultViewController alloc] init];
    SecondModel *model = [self.secondArray objectAtIndex:indexPath.row];
    resultView.secondModel = model;
    resultView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:resultView animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [resultView release];
}


- (void)startConnect
{
    NSDictionary *dic = @{@"officeId":self.secondOfficeId, @"is_traditional":@"0"};
   
    __block UITableView *table = _tabelView;
    __block NSMutableArray *myArray = self.secondArray;
    
    [MyConnection connectWithUrl:officeURL parmaters:dic finishBlock:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dataDic[@"data"];
        for (NSDictionary *dDic in array) {
            SecondModel *model = [SecondModel secondModelWithDiseaseId:dDic[@"diseaseId"] diseaseName:dDic[@"diseaseName"] diseaseDescribe:dDic[@"diseaseDescribe"] fitEat:dDic[@"fitEat"] lifeSuit:dDic[@"lifeSuit"] imgeName:dDic[@"imageName"]];
            [myArray addObject:model];
        }
        [table reloadData];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
